
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "GRTGeometry.h"

//to get the opengl function
//#include <QGLWidget>
#include <QtOpenGL\qgl.h>

//use to calculate if the mark is hoover
#include "RTMatrix44.h"

#include "GRTGLVisu.h"
#include "GRTMemoryLeak.h"
//constructor
GRTGeometry::GRTGeometry():GRTMovable(), _material()
{
	_movableType = MT_GEOMETRY;
}

//constructor
GRTGeometry::GRTGeometry(GEOMETRYTYPE type):GRTMovable(), _geoType(type), _material()
{
	_movableType = MT_GEOMETRY;
}

//destructor
GRTGeometry::~GRTGeometry(){}

//pre render function
void GRTGeometry::preRender()
{
	//store current matrix
	glPushMatrix();

	//apply translation
	glTranslatef(_translate.x, _translate.y, _translate.z);
	
	//apply rotations
	glRotatef(_rotate.x, 1, 0, 0);

	glRotatef(_rotate.y, 0, 1, 0);

	glRotatef(_rotate.z, 0, 0, 1);

	//apply scale
	glScalef(_scale.x, _scale.y, _scale.z);

	//apply material
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, _material.getAmbient());
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, _material.getDiffuse());
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, _material.getSpecular());
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, _material.getShininess());

}

//get the geometry type
GEOMETRYTYPE GRTGeometry::getType() const
{
	return _geoType;
}

//material getter
GRTMaterial& GRTGeometry::getMaterial()
{
	return _material;
}

//draw the scene without the mark and selection box
void GRTGeometry::drawAsRender()
{
	preRender();
	Render();
	glPopMatrix();
}

//return the world matrix of the primitive
RTMatrix44 GRTGeometry::getWorldMatrix()const
{
	RTMatrix44 t;
	t.createTranslation(_translate);

	RTMatrix44 rX;
	rX.createRotationX(_rotate.x * 3.14f / 180.f);
	RTMatrix44 rY;
	rY.createRotationY(_rotate.y * 3.14f / 180.f);
	RTMatrix44 rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	RTMatrix44 s;
	s.createScale(_scale);

	RTMatrix44 transform = s * rZ * rY * rX * t;
	return transform;
}

//return the world matrix with the raytracer space coordinate
RTMatrix44 GRTGeometry::getRTWorldMatrix()const
{
	RTMatrix44 t;
	t.createTranslation(_translate.x, _translate.y, -_translate.z);

	RTMatrix44 rX;
	rX.createRotationX(-_rotate.x * 3.14f / 180.f);
	RTMatrix44 rY;
	rY.createRotationY(-_rotate.y * 3.14f / 180.f);
	RTMatrix44 rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	RTMatrix44 s;
	s.createScale(_scale);

	RTMatrix44 transform = s * rZ * rY * rX * t;
	return transform;
}

//copy a material
void GRTGeometry::setMaterial(GRTMaterial& m)
{
	_material.setAmbient(m.getAmbient()[0], m.getAmbient()[1], m.getAmbient()[2], m.getAmbient()[3]);
	_material.setDiffuse(m.getDiffuse()[0], m.getDiffuse()[1], m.getDiffuse()[2], m.getDiffuse()[3]);
	_material.setSpecular(m.getSpecular()[0], m.getSpecular()[1], m.getSpecular()[2], m.getSpecular()[3]);
	_material.setShininess(m.getShininess());
	_material.setTextureName(m.getTextureFilename());
}